﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSS_Simplex
{
    public partial class Form2 : Form
    {
        private string textFilter;
        Form3 formSearch = null;

        CheckBox chkbox = new CheckBox();

        public Form2()
        {
            InitializeComponent();
            BindDatakeGrid();
        }

        private void BindDatakeGrid() 
        {
            try
            {
                DataSet ds = new DataSet();
                FileCmd.BindToDataSet("SELECT nama_var, koef_var, min_var, max_var, deskripsi FROM variable", ref ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message),"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void chkBoxChange(object sender, EventArgs e) 
        {
            for (int k = 0; k <= dataGridView1.RowCount - 1; k++) 
            {
                this.dataGridView1[0, k].Value = this.chkbox.Checked;
            }
            this.dataGridView1.EndEdit();
        }

        private void searchgrid_TextChanged(List<field> fields)
        {
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            textFilter = ""; //reset
            
            bool first = true;
            foreach (field f in fields) 
            {
                if (f.Value.Length > 0) 
                {
                    if (!first) textFilter += " and ";
                    textFilter += f.Field + " like '%" + f.Value + "%'";
                    first = false;
                }
            }
            dv.RowFilter = textFilter;
            this.label1.Text = textFilter;
        }

       private void button1_Click(object sender, EventArgs e)
        {
            formSearch = null;
            textFilter = "";
            this.label1.Text = "Filter: None";
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            dv.RowFilter = textFilter;
        }

       private void button4_Click(object sender, EventArgs e)
       {
           if (formSearch == null) 
           {
               List<field> fields = new List<field>();
               field f = new field();
               f.FriendlyName = "Var Name";
               f.Field = "nama_var";
               fields.Add(f);

               formSearch = new Form3(fields);
               formSearch.searchgrid_TextChanged += new SearchContextChangedHandler(searchgrid_TextChanged); 

           }
           formSearch.ShowDialog();
       }

       private void button3_Click(object sender, EventArgs e)
       {
           if (dataGridView1.SelectedRows.ToString() != null) 
           {
               AddSelectedDGV(dataGridView1);
           }
       }

       public static void simpanDT() 
       {
           DataGridView dataGridView1 = new DataGridView();
           DataTable dt = new DataTable();
           dt.Columns.Add("0");
           dt.Columns.Add("1");
           dt.Columns.Add("2");
           dt.Columns.Add("3");
           dt.Columns.Add("4");

           DataRow row = dt.NewRow();

           for (int i = 0; i < dataGridView1.RowCount; i++)
           {
               for (int j = 0; j < 5; j++)
               {
                   string name = Convert.ToString(dataGridView1.SelectedRows[i].Cells[j].Value);
                   Console.Write("    ");
                   Console.Write(name);
                   row[j] = name;

               }
               dt.Rows.Add(row);
               Console.WriteLine();
           }
           
       }

       private static DataGridView DGTemp,DGV1;
       public static void temporerDG(DataGridView DGView1) 
       {
           DGV1 = DGView1;
       }
       public static void AddSelectedDGV(DataGridView datagridview1)
       {
           try
           {
               List<int> listSelect = new List<int>();
               DGTemp = new DataGridView();
               int DGrow = datagridview1.SelectedRows.Count;
               int DGcol = datagridview1.ColumnCount;
               DGTemp.RowCount = DGrow;
               DGTemp.ColumnCount = DGcol;

               for (int i = 0; i < datagridview1.RowCount; i++)
               {
                   if (datagridview1.Rows[i].Selected == true)
                   {
                       listSelect.Add(i);
                   }
               }
               string[] temprow = new string[datagridview1.ColumnCount];
               for (int i = 0; i < listSelect.Count; i++) 
               {
                   for (int j = 0; j < datagridview1.ColumnCount; j++)
                   {
                        DGTemp.Rows[i].Cells[j].Value = datagridview1.Rows[listSelect.ElementAt(i)].Cells[j].Value;
                        temprow[j] = DGTemp.Rows[i].Cells[j].Value + "";
                   }
                   DGV1.Rows.Add(temprow);
               }
           }
           catch (Exception ex)
           {
               throw new Exception("Error: " + ex.Message);
           }
       }
    }
}